package com.g2one.gtunes

class UserController {

    def index = { }
	
	def register = {
		if(request.method == 'POST') {
			def u = new User(params)
			if(u.password != params.confirm) {
				u.errors.rejectValue("password", "user.password.dontmatch")
				return [user:u]
			}
			else if(u.save()) {
				session.user = u
				redirect(controller:"store")
			}
			else {
				return [user:u]
			}
		}
	}
	
	def login = { LoginCommand cmd ->
		if(request.method == 'POST') {
			if(!cmd.hasErrors()) {
				session.user = cmd.getUser()
				redirect(controller:'store')
			}
			else {
				render(view:'/store/index', model:[loginCmd:cmd])
			}
		}
		else {
			render(view:'/store/index')
		}
	}
}
